<?php

require("model/Datos.php");
require("model/Etiqueta.php");

class Foto
{
	var $tabla = 'fotos';
	
	var $value = array(
						'id'		=>'0',
						'nombre'	=>'nombre',
						'dir'		=>'dir',
						'etiquetas'	=>'etiquetas'
					);
					
	function index($where=null){

		$datos = new Datos();
		
		$datos->conectar();
		
		$query = "SELECT ";
		
		$query2 = " FROM " . $this->tabla;
		
		foreach($this->value as $k=>$v){
		
			$query .=  $k . ",";
									
		}
				
		$query = substr($query, 0, strlen($query)-1);
		
		if(!empty($where)){
			
			$query3 = '';
			
			foreach($where as $k=>$v){
			
				$query3 .= $k  . " like '%" . $v . "%' AND";
						
			}

			$query2 .= " WHERE " . substr($query3, 0, strlen($query3)-3);
		}
		
		$query .= $query2;
//echo $query;		
		$data = mysql_db_query($datos->database, $query);
		
		$fotos = array();

		if(!empty($data))

		while($i = mysql_fetch_array($data)) {
		
			$img = array(
						'id'		=> $i['id'],
						'nombre'	=> $i['nombre'],
						'dir'		=> $i['dir'],
						'etiquetas'	=> $i['etiquetas']
					);
			$fotos[] = $img;

		}
				
		$datos->desconectar();
		
		return $fotos;		
	}
		
	function save($val){
		
		$datos = new Datos();
		
		$datos->conectar();
		
		$query = "INSERT INTO ". $this->tabla ."(";
		
		$query2 = " VALUES(";
		
		$coma = count($val);
		
		foreach($val as $k=>$v){
		
			$query .=  $k . ",";
			
			$query2 .= "'" . $v . "',";
						
		}
		
		$query = substr($query, 0, strlen($query)-1) . ")" . substr($query2, 0, strlen($query2)-1) . ");";
		
		$status = mysql_query($query);	
				
		$datos->desconectar();
		
		if($status){
		
			$etiqueta = new Etiqueta();
			
			$etiqueta->save($val['etiquetas']);			
		}
				
		return $status;
	}
	
	function edit(){
		
	}
	
	function delete($id){

		$foto2delete = $this->index(array('id'=>$id));
		
		$etiquetas = $foto2delete[0]['etiquetas'];
		
		$where = array('id'=>$id);
		
		$datos = new Datos();
		
		$datos->conectar();
		
		$query = "DELETE FROM " . $this->tabla;
				
		$query2 = '';
		
		if(!empty($where)){
			
			foreach($where as $k=>$v){
			
				$query2 .= $k  . "='" . $v . "' AND";
						
			}
			$query2 .= " WHERE " . substr($query2, 0, strlen($query2)-3);
		}
		
		$query .= $query2;
		
		$data = mysql_db_query($datos->database, $query);
				
		$datos->desconectar();		
		
	}
	

	
	function valid($foto){
		
	}
}

?>